package cn.botter.auth.common.entity;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
@Slf4j
@Data
public class PageResult<T> {
    private Integer pageNo = 1;
    private Integer pageSize = 20;
    private Integer total = 0;
    private Integer totalPage = 0;
    private List<T> result = Collections.emptyList();
    private Integer start = 1;
    private Integer end = 0;
    public void setTotal(Integer total){
        if (total == null || total < 0) {
            if (this.pageSize == null || this.pageSize < 0 || this.pageSize >= 100000 ){
                log.info("PageResult.setTotal 中pageSize的值不合法");
                return;
            }
            log.info("PageResult.setTotal 中total的值不合法");
            return;
        }
        this.total = total;
        //总的页数
        this.totalPage = this.total/this.pageSize + (this.total % this.pageSize == 0 ? 0 : 1 );
        this.start = (this.pageNo-1) * this.pageSize + 1;
        this.end = (this.start-1+this.pageSize * (this.pageNo > 0 ? 1 : 0));
    }

    public void setPageNo(Integer pageNo){
        if (pageNo == null || pageNo < 1){
            this.pageNo = 1;
            return;
        }
        this.pageNo = pageNo;
    }

    public void setPageSize(Integer pageSize){
        if (pageSize == null || pageSize < 1 || pageSize > Integer.MAX_VALUE-1){
            this.pageSize = 1;
            return;
        }
        this.pageSize = pageSize;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }
}
